package com.yuanluludev.lib_pic.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.yuanluludev.lib_pic.R;
import com.yuanluludev.lib_xpopup.util.XPopupUtils;

/**
 * yuanLulu
 * RecyclerGridImageView
 */
public class RecyclerGridImageView extends RecyclerView {

    private int spanCount = 3;
    private int dividerWidth = 5;

    public RecyclerGridImageView(@NonNull Context context) {
        super(context);
        init(null);
    }

    public RecyclerGridImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public RecyclerGridImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {

        if (attrs != null) {
            TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.RecyclerGridImageView);
            spanCount = attributes.getInt(R.styleable.RecyclerGridImageView_span_count, spanCount);
            dividerWidth = attributes.getInt(R.styleable.RecyclerGridImageView_divider_width, dividerWidth);
        }

        setLayoutManager(new FullyGridLayoutManager(getContext(), spanCount, GridLayoutManager.VERTICAL, false));
        addItemDecoration(new DividerGridItemDecoration(getContext(), -1, XPopupUtils.dp2px(getContext(), dividerWidth), XPopupUtils.dp2px(getContext(), dividerWidth), Color.WHITE, false));
    }
}
